package xyz.yuzegod.megawalls.nms;

import net.minecraft.server.v1_8_R3.EntityLiving;
import net.minecraft.server.v1_8_R3.EntityPig;
import net.minecraft.server.v1_8_R3.EntityPlayer;
import net.minecraft.server.v1_8_R3.World;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
import org.bukkit.entity.Pig;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.plugin.Plugin;
import xyz.yuzegod.megawalls.MegaWalls;
import xyz.yuzegod.megawalls.game.GamePlayer;
import xyz.yuzegod.megawalls.util.PlayerUtils;

public class CustomPig extends EntityPig implements CustomEntity, Runnable
{
    private GamePlayer gamePlayer;
    private int taskId;
    
    public CustomPig(final World world) {
        super(world);
        this.taskId = 0;
        this.taskId = Bukkit.getScheduler().runTaskTimer((Plugin)MegaWalls.getInstance(), (Runnable)this, 0L, 5L).getTaskId();
    }
    
    public GamePlayer getGamePlayer() {
        if (this.gamePlayer != null) {
            return this.gamePlayer;
        }
        final String name = ChatColor.stripColor(this.getCustomName());
        final Player player = Bukkit.getPlayer(name);
        if (player == null) {
            return null;
        }
        return this.gamePlayer = GamePlayer.get(player.getUniqueId());
    }
    
    public void setGamePlayer(final GamePlayer gamePlayer) {
        this.gamePlayer = gamePlayer;
        this.setCustomName(gamePlayer.getDisplayName(null));
        this.setCustomNameVisible(true);
        ((Pig)this.getBukkitEntity()).setCanPickupItems(false);
        ((Pig)this.getBukkitEntity()).setSaddle(true);
    }
    
    public void setGoalTarget(final EntityLiving entityliving) {
        this.setGoalTarget(entityliving, EntityTargetEvent.TargetReason.CLOSEST_PLAYER, false);
    }
    
    public void setGoalTarget(final EntityLiving entityliving, final EntityTargetEvent.TargetReason reason, final boolean fire) {
        if (!(entityliving instanceof EntityPlayer)) {
            return;
        }
        final GamePlayer gamePlayer = this.getGamePlayer();
        if (gamePlayer.getGameTeam() != null && gamePlayer.getGameTeam().isInTeam(GamePlayer.get(((EntityPlayer)entityliving).getBukkitEntity().getUniqueId()))) {
            return;
        }
        super.setGoalTarget(entityliving, reason, fire);
    }
    
    protected void dropDeathLoot(final boolean flag, final int i) {
    }
    
    public void run() {
        for (final Player player : PlayerUtils.getNearbyPlayers(this.getBukkitEntity().getLocation(), 10.0)) {
            if (player == null) {
                continue;
            }
            final GamePlayer gamePlayer = GamePlayer.get(player.getUniqueId());
            if (gamePlayer == null || gamePlayer.isSpectator()) {
                continue;
            }
            if (this.getGamePlayer().getGameTeam().isInTeam(gamePlayer)) {
                continue;
            }
            this.setGoalTarget((EntityLiving)((CraftPlayer)player).getHandle());
        }
    }
    
    public void die() {
        Bukkit.getScheduler().cancelTask(this.taskId);
        super.die();
    }
}
